﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsFlower
{
    public class Petal
    {
        public Point Position { get; set; }
        public Size Size { get; set; }
        public Color Color { get; set; }
        public float Rotation { get; set; }
        public float RotationSpeed { get; set; }
        public float FallSpeed { get; set; }
        public float SwingAmount { get; set; }
        public float SwingSpeed { get; set; }
        public float SwingPhase { get; set; }

        public Petal(Point startPosition, Random random)
        {
            Position = startPosition;
            Size = new Size(random.Next(15, 30), random.Next(15, 30));
            Color = GetRandomPetalColor(random);
            Rotation = 0;
            RotationSpeed = (float)(random.NextDouble() * 2 - 1); // -1 到 1
            FallSpeed = (float)(random.NextDouble() * 2 + 1); // 1 到 3
            SwingAmount = (float)(random.NextDouble() * 3 + 1); // 1 到 4
            SwingSpeed = (float)(random.NextDouble() * 0.05 + 0.02); // 0.02 到 0.07
            SwingPhase = (float)(random.NextDouble() * Math.PI * 2);
        }

        private Color GetRandomPetalColor(Random random)
        {
            Color[] petalColors =
            {
            Color.FromArgb(255, 182, 193), // LightPink
            Color.FromArgb(255, 105, 180), // HotPink
            Color.FromArgb(255, 192, 203), // Pink
            Color.FromArgb(255, 160, 122), // LightSalmon
            Color.FromArgb(240, 128, 128), // LightCoral
            Color.FromArgb(255, 218, 185), // PeachPuff
            Color.FromArgb(255, 239, 213), // PapayaWhip
        };
            return petalColors[random.Next(petalColors.Length)];
        }

        public void Update()
        {
            // 下落
            Position = new Point(Position.X, Position.Y + (int)FallSpeed);

            // 摆动（左右移动）
            float swing = (float)Math.Sin(SwingPhase) * SwingAmount;
            Position = new Point(Position.X + (int)swing, Position.Y);

            // 旋转
            Rotation += RotationSpeed;

            // 更新摆动相位
            SwingPhase += SwingSpeed;
            if (SwingPhase > Math.PI * 2)
                SwingPhase -= (float)(Math.PI * 2);
        }

        public bool IsOutOfBounds(Rectangle bounds)
        {
            return Position.Y > bounds.Height;
        }
    }
}
